<?php

namespace app\helpers;

use app\components\PriceUnit;
use app\models\System;

/**
 * 订单记录信息基础
 * Class SaleRcdBase
 * @package app\common\model
 */
class Coin
{
    const UNIT_CNY = 1;
    const UNIT_THB = 2;
    const UNIT_USD = 3;

    protected $autoWriteTimestamp = true;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'unit' => [
                self::UNIT_CNY => '人民币(CNY)',
                self::UNIT_THB => '泰铢(THB)',
                self::UNIT_USD => '美元(USD)',
            ],
            'unit_label' => [
                self::UNIT_CNY => '元',
                self::UNIT_THB => 'THB',
                self::UNIT_USD => 'USD',
            ]
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) && isset($dropDownList[$column][$value]) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }

    public static function getValueToPrice($value, $unit)
    {
        $prefix = doubleval($value) < 0 ? '-' : '';
        $value = abs($value);
        switch ($unit) {
            case self::UNIT_CNY:
                return $prefix . '¥' . $value;
                break;
            case self::UNIT_THB:
                return $prefix . '฿' . $value;
                break;
            case self::UNIT_USD:
                return $prefix . '$' . $value;
                break;
            default:
                return $prefix . $value . self::dropdown('unit_label', $unit);
        }
    }

    public static function getUnit($unit)
    {
        switch ($unit) {
            case self::UNIT_CNY:
                return '¥';
                break;
            case self::UNIT_THB:
                return '฿';
                break;
            case self::UNIT_USD:
                return '$';
            default:
                return self::dropdown('unit_label', $unit);
        }
    }

    public static function call_api($scur, $tcur)//汇率获取
    {
        $api_url = "http://api.k780.com:88/?app=finance.rate&scur=" . $scur . "&tcur=" . $tcur . "&appkey=51184&sign=8227de5020a71208ed66eb9b3cd11199&format=json";
        if (!$get_api = file_get_contents($api_url)) {
            die('远程API故障');
        }
        if (!$arr_api = json_decode($get_api, true)) {
            die('JSON转码失败');
        }
        if ($arr_api['success'] != '1') {
            die($arr_api['msg']);
        }
        return $arr_api['result'];
    }

    public static function getValueToCny($value, $unit)
    {
        switch ($unit) {
            case self::UNIT_THB:
                return $value * System::getSystem()->thb_cny_rate;
            case self::UNIT_USD:
                return $value * System::getSystem()->usd_cny_rate;
            default:
                return $value;
        }
    }

    public static function getValueToThb($value, $unit)
    {
        switch ($unit) {
            case self::UNIT_CNY:
                return $value * System::getSystem()->cny_thb_rate;
            case self::UNIT_USD:
                return $value * System::getSystem()->usd_thb_rate;
            default:
                return $value;
        }
    }

    /**
     * @param $value //必须是泰铢单位
     * @return mixed
     */
    public static function getSharePriceLabel($value, $is_show = false)
    {
        return self::getValueToPrice(round(self::getShowPrice($value, $is_show), 1), self::getShowPriceUnit(self::UNIT_THB, $is_show));
    }

    /**
     * @param $value //必须是泰铢单位
     * @return mixed
     */
    public static function getShowPriceLabel($value, $is_show = false)
    {
        return self::getValueToPrice(round(self::getShowPrice($value, $is_show), 2), self::getShowPriceUnit(self::UNIT_THB, $is_show));
    }

    /**
     * @param $value //必须是泰铢单位
     * @return mixed
     */
    public static function getShowPriceLabelFromUnit($value, $price_unit)
    {
        return self::getValueToPrice(round(self::getShowPriceFromUnit($value, $price_unit), 2), $price_unit);
    }

    /**
     * @param $value //必须是泰铢单位
     * @return mixed
     */
    public static function getShowPrice($value, $is_show = false)
    {
        if (!$is_show)
            return $value;

        return self::getShowPriceFromUnit($value, PriceUnit::getTotal());
    }

    public static function getShowPriceFromUnit($value, $m_price_unit)
    {
        switch ($m_price_unit) {
            case self::UNIT_THB:
                return $value;
            case self::UNIT_CNY:
                return $value * System::getSystem()->thb_cny_rate;
            case self::UNIT_USD:
                return $value * System::getSystem()->thb_usd_rate;
        }
    }

    /**
     * @param $value //必须是泰铢单位
     * @return mixed
     */
    public static function getShowPriceUnit($value, $is_show = false)
    {
        if (!$is_show)
            return $value;

        return intval(PriceUnit::getTotal());
    }
}
